#include "EventHandlerSimulationNode.hpp"
#include "TimeManager.hpp"

EventHandlerSimulationNode::EventHandlerSimulationNode()
{
	mMessageRouter.registerHandler(
		Timer::EFromNodeJoinTimeLoop,
		[this](Message& msg) { onFromNodeJoinTimeLoop(msg); });
	//mMessageRouter.registerHandler(
	//	Agent::ETaskRunningFromAgent,
	//	[this](Message& msg) { onFromTaskRunningFromAgent(msg); });
	mMessageRouter.registerHandler(
		Timer::EFromNodeLeaveTimeLoop,
		[this](Message& msg) { onFromNodeLeaveTimeLoop(msg); });
	mMessageRouter.registerHandler(
		Timer::EFromNodeTimeStepConfirm,
		[this](Message& msg) { onFromNodeTimeStepConfirm(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromNodeRequestIdRange,
		[this](Message& msg) { onFromNodeRequestIdRange(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromNodeSubscribeHotArea,
		[this](Message& msg) { onFromNodeSubscribeHotArea(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromNodeUnsubscribeHotArea,
		[this](Message& msg) { onFromNodeUnsubscribeHotArea(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromAnyCreateHotArea,
		[this](Message& msg) { onFromAnyCreateHotArea(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromAnyUpdateHotArea,
		[this](Message& msg) { onFromAnyUpdateHotArea(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromAnyDeleteHotArea,
		[this](Message& msg) { onFromAnyDeleteHotArea(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromAnyAddDeleteTrafficActors,
		[this](Message& msg) { onFromAnyAddDeleteTrafficActors(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromAnyUpdateTrafficActorPositionRotation,
		[this](Message& msg) { onFromAnyUpdateTrafficActorPositionRotation(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromAnyUpdateTrafficActorFlags,
		[this](Message& msg) { onFromAnyUpdateTrafficActorFlags(msg); });
	mMessageRouter.registerHandler(
		HotArea::EFromAnyUpdateTrafficActorCustomProperties,
		[this](Message& msg) { onFromAnyUpdateTrafficActorCustomProperties(msg); });

	mMessageRouter.registerHandler(
		HotArea::EFromServerResultHotAreaCenterActors,
		[this](Message& msg) { onFromServerResultHotAreaCenterActors(msg); });

	mMessageRouter.registerHandler(
		Timer::EFromConsolePlay,
		[this](Message& msg) {
		onFromConsoleCommand(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromConsoleReset,
		[this](Message& msg) {
		onFromConsoleCommand(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromConsoleSetFPS,
		[this](Message& msg) {
		onFromConsoleCommand(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromConsolePause,
		[this](Message& msg) {
		onFromConsoleCommand(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromConsoleStepForwardMS,
		[this](Message& msg) {
		onFromConsoleCommand(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromConsoleStepForwardFrame,
		[this](Message& msg) {
		onFromConsoleCommand(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromConsoleStop,
		[this](Message& msg) {
		onFromConsoleCommand(msg);
	});
	mMessageRouter.registerHandler(
		NodeCommon::EFromAnyToNodeCommand,
		[this](Message& msg) {
		onFromAnyToNodeCommand(msg);
	});
	mMessageRouter.registerHandler(
		NodeCommon::EFromAnyToAllCommand,
		[this](Message& msg) {
		onFromAnyToAllCommand(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromNodeRequestTimerPause,
		[this](Message& msg) {
		onFromNodeRequestTimerPause(msg);
	});
	mMessageRouter.registerHandler(
		Timer::EFromNodeRequestTaskEnd,
		[this](Message& msg) {
		onFromNodeRequestTaskEnd(msg);
	});

	mMessageRouter.registerHandler(
		Timer::EFromCustomChannelCreate,
		[this](Message& msg) {
		onFromCustomChannelCreate(msg);
	});

	mMessageRouter.registerHandler(
		Timer::EFromCustomChannelSubscribe,
		[this](Message& msg) {
		onFromCustomChannelSubscribe(msg);
	});

	mMessageRouter.registerHandler(
		Timer::EFromCustomChannelSendMessage,
		[this](Message& msg) {
		onFromCustomChannelSendMessage(msg);
	});

}

EventHandlerSimulationNode::~EventHandlerSimulationNode()
{
}

void EventHandlerSimulationNode::onDisposed()
{
}

void EventHandlerSimulationNode::onDisconnected()
{
	Message msgUnregister;
	msgUnregister.create(Timer::EFromNodeUnregister, 0);

	gTimeManager->queueTimeLoopCommand(getSocket(), msgUnregister);
	gTimeManager->queueControlCommand(getSocket(), msgUnregister);
}

void EventHandlerSimulationNode::onConnected()
{
}

void EventHandlerSimulationNode::onMessage(Message& msg)
{
	mMessageRouter.onMessage(msg);
}

void EventHandlerSimulationNode::onFromNodeJoinTimeLoop(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}
void EventHandlerSimulationNode::onFromTaskRunningFromAgent(Message& msg) {
	gTimeManager->queueControlCommand(getSocket(), msg);
}
void EventHandlerSimulationNode::onFromAnyRequestTimeRun(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromNodeLeaveTimeLoop(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromNodeRequestIdRange(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromNodeSubscribeHotArea(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromNodeUnsubscribeHotArea(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromNodeTimeStepConfirm(Message& msg)
{
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyCreateHotArea(Message& msg)
{
	logInfo("---------onFromAnyCreateHotArea--------");
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyUpdateHotArea(Message& msg)
{
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyDeleteHotArea(Message& msg)
{
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyAddDeleteTrafficActors(Message& msg)
{
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyUpdateTrafficActorPositionRotation(Message& msg)
{
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyUpdateTrafficActorFlags(Message& msg)
{
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyUpdateTrafficActorCustomProperties(Message& msg)
{
	gTimeManager->queueTimeLoopCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyToNodeCommand(Message & msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromAnyToAllCommand(Message & msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromConsoleCommand(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromNodeRequestTimerPause(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromNodeRequestTaskEnd(Message& msg)
{
	gTimeManager->queueControlCommand(getSocket(), msg);
}

void EventHandlerSimulationNode::onFromCustomChannelCreate(Message& msg) {
	gTimeManager->queueControlCommand(getSocket(), msg);
}
void EventHandlerSimulationNode::onFromCustomChannelSubscribe(Message& msg) {
	gTimeManager->queueControlCommand(getSocket(), msg);
}
void EventHandlerSimulationNode::onFromCustomChannelSendMessage(Message& msg) {
	gTimeManager->queueControlCommand(getSocket(), msg);
}
void EventHandlerSimulationNode::onFromServerResultHotAreaCenterActors(Message& msg) {
	gTimeManager->queueControlCommand(getSocket(), msg);
}
